<template>
  <div class="box-padding"
       ref="echartsWrapper">
    <Row :gutter="16">
      <Col span="6"><span style="font-size: 14px;color: #697786">风险总数：{{riskCount}}条；措施方案：{{measureCount}}条</span></Col>
      <Col span="4"
           offset="6"  v-if="this.personType === 1">
      <Select v-model="formData.enterpriseId" style="width: 90%;" ref="treeSelect" placeholder="机构">
      							<Option v-for="item in list1" :value="item.enterpriseId" :key="item.enterpriseId" style="display: none;">{{ item.title }}</Option>
      							<Tree :data="orgList" style="padding:0 10px" @on-select-change="checkEng"></Tree>
      						</Select>
      </Col>

      <Col span="4"  v-if="this.personType === 1">
        <Select v-if="this.personType != 3" clearable v-model="formData.projectTeamId" style="width: 90%;" placeholder="全部项目" @on-change="proChange">
          <Option v-for="item in proList" :value="item.id" :key="item.id">{{ item.shortName }}</Option>
        </Select>
        <Select v-if="this.personType == 3"  :clearable="(proList.length>1)" v-model="formData.projectTeamId" style="width: 90%;" placeholder="全部项目" @on-change="proChange">
          <Option v-for="item in proList" :value="item.id" :key="item.id">{{ item.shortName }}</Option>
        </Select>
       </Col>
       <Col span="4"  v-if="this.personType !== 1" offset="10" >
           <Select v-if="this.personType != 3" clearable v-model="formData.projectTeamId" style="width: 90%;" placeholder="全部项目" @on-change="proChange">
             <Option v-for="item in proList" :value="item.id" :key="item.id">{{ item.shortName }}</Option>
           </Select>
           <Select v-if="this.personType == 3" :clearable="(proList.length>1)" v-model="formData.projectTeamId" style="width: 90%;" placeholder="全部项目" @on-change="proChange">
             <Option v-for="item in proList" :value="item.id" :key="item.id">{{ item.shortName }}</Option>
           </Select>
       </Col>
      <Col span="4" >
        <DatePicker @on-change="dateChange" type="month" :value="formData.date"  style="width: 90%" placeholder="日期" :clearable="false"></DatePicker>
      </Col>
    </Row>
    <first-line :line="riskLevelData" :pie="measureTypeData" :pie1="measureSourceData" v-if="showPic"></first-line>
    <Row :gutter="16" style="padding-top: 20px">
      <Col span="20"><span style="font-size: 14px;color: #697786">技术措施：{{techCount}}条/安全风险：{{safeCount}}条</span></Col>
      <Col span="4">
      <Select v-model="formData.type" @on-change="getTechAndSafe">
        <Option v-for="item in safeOptions"
                :value="item.id"
                :key="item.id">{{ item.name }}</Option>
      </Select>
      </Col>
    </Row>
    <Table border
           :columns="columns"
           :data="data"
           style="margin: 20px 0;">
    </Table>
    <Row style="padding-bottom: 25px">
      <Col span="24"
           >
      <span style="font-size: 14px;color: #697786">管控总数：{{allDoCount}}条；已管控数：{{firstDoCount}}条； 待管控数: {{firstUnDoCount}}条；</span>
      <span style="font-size: 14px;color: #697786">管控结果到位：{{secondDoCount}}条； 管控结果不到位： {{seconUnDoCount}}条</span>
      </Col>
    </Row>
    <second-line :pie2="thirdStatisticsFirst" :pie3="thirdStatisticsSecond" v-if="showPic2"></second-line>
    <third-line :data1="line1" :data2="line2" :name="lineName" v-if="showPic3">
    </third-line>
  </div>
</template>
<script>
import firstLine from './first.vue'
import secondLine from './second.vue'
import thirdLine from './third.vue'
export default {
  name:'statistics-dynamics',
  components: {
    firstLine,
    secondLine,
    thirdLine
  },
  data() {
    return {

      showPic: false,
      showPic2: false,
      showPic3: false,
      allDoCount:0,
      firstDoCount:0,
      firstUnDoCount:0,
      secondDoCount:0,
      seconUnDoCount:0,
      userDetail: JSON.parse(localStorage.getItem('userList')),
      personType: JSON.parse(localStorage.getItem('personType')),
      orgList: [], //机构树
      proList: [], //项目部树
      model1: '',
      list1: [],
      list2: [],
      monthList: [],
      data: [],
      safeCount: 0, //安全风险
      techCount: 0, //技术措施
      measureCount: 0, //措施方案
      riskCount: 0, //风险总数
      formData: {
        type: 1,
        date:new Date().getFullYear() + "-" + ('00'+(new Date().getMonth() + 1)).slice(-2)
      },
      riskLevelData: [],
      measureSourceData: [],
      measureTypeData: [],
      thirdStatisticsFirst:[],
      thirdStatisticsSecond:[],
      lineName: [],
      line1: [],
      line2: [],
      safeOptions: [
        {name: '技术措施', id: 1},
        {name: '安全风险', id: 0},
      ],
      columns: [
        {
          title: '序号',
          align: 'center',
          width: 70,
          render: (h, params) => {
            return h(
              'span',
              params.index + 1
            )
          }
        },
        {
          title: '风险类型',
          key: 'name',
          align: 'center'
        },
        {
          title: '合计',
          key: 'rowCount',
          align: 'center'
        },
        {
          title: 'I级',
          key: 'firstLevel',
          align: 'center'
        },
        {
          title: 'II级',
          key: 'secondLevel',
          align: 'center'
        },
        {
          title: 'III级',
          key: 'thirdLevel',
          align: 'center'
        },
        {
          title: 'IV级',
          key: 'fourthLevel',
          align: 'center'
        }
      ]
    }
  },
  mounted() {
    //1集团 2工程公司 3项目部
    if(this.personType === 3) {

      this.formData.enterpriseId = this.userDetail.projectTeamId;
      this.getProTree({
        objectId: this.userDetail.projectTeamId
      })

    } else if(this.personType === 1) {
      this.getOrgTree();
    } else if(this.personType === 2) {

      this.formData.enterpriseId = this.userDetail.enterpriseId;
      this.getProTree({
        objectId: this.userDetail.enterpriseId
      })

    }

  },
  methods: {
        //机构树
  			getOrgTree() {
  				this.orgList = [];
  				this.list1 = [];
  				this.$api.measurePlan.getOrgTree().then((res) => {
  					if(res.code == 200) {
  						this.orgList = res.data;
  						this.checkEng(this.orgList);
  					} else {
  						this.$Message.error(res.data.msg);
  					}
  				})
  			},
  			//点击机构树
        checkEng(data) {
          if(data.length > 0) {
            this.formData.projectTeamId = '';
            this.list1 = []
            let title = data[0].title
            if(this.$route.query.orgId){
              let enterpriseId = this.$route.query.orgId
              this.list1.push({
                title,
                enterpriseId
              })
              this.getProTree(data[0]);
              this.formData.enterpriseId = enterpriseId
              this.$refs.treeSelect.visible = false
              this.getDymanicPic()
            }else if(data[0].objectId) {
              let enterpriseId = data[0].objectId
              this.list1.push({
                title,
                enterpriseId
              })
              this.getProTree(data[0]);
              this.formData.enterpriseId = enterpriseId
              this.$refs.treeSelect.visible = false
              this.getDymanicPic()
            } else {
              this.$Message.warning('请选择' + title + '下的子集')
            }
          }

        },
        //获取项目
        getProTree(data) {
          this.proList = [];
          this.formData.projectTeamId = '';
          this.$api.measurePlan.getProList(data.objectId).then(res => {
            if(res.code === 200) {
              if(res.data.length > 0) {
                res.data.forEach(item => {
                  this.proList.push(item);
                });
                if(this.proList.length == 1 && this.personType === 3) {
                  this.formData.projectTeamId = this.proList[0].id;
                }
                this.getDymanicPic()
                this.getMeasure()
                this.getTechAndSafe()
              }
            } else {
              this.$Message.error(res.message)
            }
          })
        },
        checkPro(data) { //点击项目树
            if(data.length > 0) {
              this.list2 = []
              let proId = data[0].id
            let title = data[0].shortName
              this.list2.push({
                proId,
                title
              })
              this.formData.projectId = proId
            this.$refs.treeSelectPro.visible = false
            }
          },
          getDymanicPic() { //过程管控三种图表统计
              this.showPic2 = false
              this.showPic3 = false

              let params = {
                "orgId": this.formData.enterpriseId,
                 "proId": this.formData.projectId || '',
                 "controlTime": this.formData.date
              }
              this.$api.dymanic.getDymanicPic(params).then(result=> {
                if(result.code == 200) {
                  this.thirdStatisticsFirst = Object.values(result.data.thirdStatistics.first)
                  this.thirdStatisticsSecond = Object.values(result.data.thirdStatistics.second)
                  let sum = 0
                  this.thirdStatisticsFirst.forEach(item=>{
                    sum += item
                  })
                  if(sum == 0) {
                    this.thirdStatisticsFirst = []
                  }
                  let sum1 = 0
                  this.thirdStatisticsSecond.forEach(item=>{
                    sum1 += item
                  })
                  if(sum1 == 0) {
                    this.thirdStatisticsSecond = []
                  }
                  if(result.data.fourthStatistics && result.data.fourthStatistics.length > 0) {
                      this.lineName = []
                      this.line1 = []
                      this.line2 = []
                      result.data.fourthStatistics.forEach(item=>{
                        this.lineName.push(item.name)
                        this.line1.push(item.value[0])
                        this.line2.push(item.value[1])
                      })
                      let num = 0
                      let num2 = 0
                      this.line1.forEach(item=>{
                        num += item
                      })
                      this.line2.forEach(item=>{
                        num2 += item
                      })

                      if(num + num2 == 0) {
                        this.lineName = []
                         this.line1 = []
                         this.line2 = []
                      }
                  } else {
                      this.lineName = []
                       this.line1 = []
                       this.line2 = []
                  }
                  this.allDoCount = result.data.allDoCount
                  this.firstDoCount = result.data.firstDoCount
                  this.firstUnDoCount = result.data.firstUnDoCount
                  this.secondDoCount = result.data.secondDoCount
                  this.seconUnDoCount = result.data.seconUnDoCount
                  this.showPic2 = true
                  this.showPic3 = true
                } else {
                  this.thirdStatisticsFirst = []
                  this.thirdStatisticsSecond = []
                  this.lineName = []
                  this.line1 = []
                  this.line2 = []
                  this.allDoCount = result.data.allDoCount
                  this.firstDoCount = result.data.firstDoCount
                  this.firstUnDoCount = result.data.firstUnDoCount
                  this.secondDoCount = result.data.secondDoCount
                  this.seconUnDoCount = result.data.seconUnDoCount
                  this.showPic2 = true
                  this.showPic3 = true
                  this.$Message.warning(res.message)
                }
              }).catch(() => {
                  this.showPic2 = true
                  this.showPic3 = true
                  this.$Message.warning('请求图表失败')
              })
          },
          getMeasure() { //风险总数、措施方案统计
              this.showPic = false

              let params = {
                "orgId": this.formData.enterpriseId,
                 "proId": this.formData.projectId || '',
                 "controlTime": this.formData.date
              }
              this.$api.dymanic.getMeasure(params).then(res => {
                if(res.code == 200) {
                  this.riskLevelData = Object.values(res.data.riskLevelData)
                  this.measureSourceData = Object.values(res.data.measureSourceData)
                  this.measureTypeData = Object.values(res.data.measureTypeData)
                  this.measureCount = res.data.measureCount
                  this.riskCount = res.data.safeCount

                  let noData1 = true
                  let noData2 = true
                  let noData3 = true
                  this.riskLevelData.forEach(item=>{
                    if(item > 0) {
                     noData1 = false
                    }
                  })
                  this.measureSourceData.forEach(item=>{
                    if(item > 0) {
                     noData2 = false
                    }
                  })
                  this.measureTypeData.forEach(item=>{
                    if(item > 0) {
                      noData3 = false
                    }
                  })
                  if(noData1) {
                    this.riskLevelData = []
                  }
                  if(noData2) {
                    this.measureSourceData = []
                   }
                  if(noData3) {
                    this.measureTypeData = []
                  }
                  this.showPic = true
                } else {
                    this.riskLevelData = []
                    this.measureSourceData = []
                    this.measureTypeData = []
                    this.measureCount = 0
                    this.riskCount = 0
                    this.showPic = true
                    this.$Message.error(res.message)
                }
              }).catch(() => {
                  this.showPic = true
                  this.$Message.warning('请求图表失败')
              })
          },
          getTechAndSafe() { //技术措施、安全风险统计

            let params = {
              "orgId": this.formData.enterpriseId,
               "proId": this.formData.projectId || '',
               "controlTime": this.formData.date,
               "riskType": this.formData.type + ''   //"0:安全风险，1:技术措施"
            }
            this.$api.dymanic.getTechAndSafe(params).then(result=> {
              if(result.code == 200) {
                if(result.data.techCount) {
                    this.data = result.data.data
                    this.safeCount = result.data.safeCount
                    this.techCount = result.data.techCount
                } else {
                    this.data = []
                    this.safeCount = 0
                    this.techCount = 0
                }
              } else {
                    this.data = []
                    this.safeCount = 0
                    this.techCount = 0
                    this.$Message.error(res.message)
              }
            }).catch(() => {
                this.$Message.warning('请求风险类型失败')
            })
        },
        proChange() { //点击项目
          this.getDymanicPic()
          this.getMeasure()
          this.getTechAndSafe()
        },
        dateChange(date){
          this.formData.date = date;
          this.getDymanicPic()
          this.getMeasure()
          this.getTechAndSafe()
        },
  }
}
</script>
<style scoped>
 .ivu-input-suffix {
    display: none !important
 }
</style>